package com.health.healthSys.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.health.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.health.healthSys.domain.HealthInfo;
import com.health.healthSys.mapper.InfoCategoryMapper;
import com.health.healthSys.domain.InfoCategory;
import com.health.healthSys.service.IInfoCategoryService;

/**
 * 资讯分类Service业务层处理
 * 
 * @author maple
 * @date 2025-02-28
 */
@Service
public class InfoCategoryServiceImpl implements IInfoCategoryService 
{
    @Autowired
    private InfoCategoryMapper infoCategoryMapper;

    /**
     * 查询资讯分类
     * 
     * @param categoryId 资讯分类主键
     * @return 资讯分类
     */
    @Override
    public InfoCategory selectInfoCategoryByCategoryId(String categoryId)
    {
        return infoCategoryMapper.selectInfoCategoryByCategoryId(categoryId);
    }

    /**
     * 查询资讯分类列表
     * 
     * @param infoCategory 资讯分类
     * @return 资讯分类
     */
    @Override
    public List<InfoCategory> selectInfoCategoryList(InfoCategory infoCategory)
    {
        return infoCategoryMapper.selectInfoCategoryList(infoCategory);
    }

    /**
     * 新增资讯分类
     * 
     * @param infoCategory 资讯分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInfoCategory(InfoCategory infoCategory)
    {
        int rows = infoCategoryMapper.insertInfoCategory(infoCategory);
        insertHealthInfo(infoCategory);
        return rows;
    }

    /**
     * 修改资讯分类
     * 
     * @param infoCategory 资讯分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInfoCategory(InfoCategory infoCategory)
    {
        infoCategoryMapper.deleteHealthInfoByCategoryId(infoCategory.getCategoryId());
        insertHealthInfo(infoCategory);
        return infoCategoryMapper.updateInfoCategory(infoCategory);
    }

    /**
     * 批量删除资讯分类
     * 
     * @param categoryIds 需要删除的资讯分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInfoCategoryByCategoryIds(String[] categoryIds)
    {
        infoCategoryMapper.deleteHealthInfoByCategoryIds(categoryIds);
        return infoCategoryMapper.deleteInfoCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除资讯分类信息
     * 
     * @param categoryId 资讯分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInfoCategoryByCategoryId(String categoryId)
    {
        infoCategoryMapper.deleteHealthInfoByCategoryId(categoryId);
        return infoCategoryMapper.deleteInfoCategoryByCategoryId(categoryId);
    }

    /**
     * 新增资讯管理信息
     * 
     * @param infoCategory 资讯分类对象
     */
    public void insertHealthInfo(InfoCategory infoCategory)
    {
        List<HealthInfo> healthInfoList = infoCategory.getHealthInfoList();
        String categoryId = infoCategory.getCategoryId();
        if (StringUtils.isNotNull(healthInfoList))
        {
            List<HealthInfo> list = new ArrayList<HealthInfo>();
            for (HealthInfo healthInfo : healthInfoList)
            {
                healthInfo.setCategoryId(categoryId);
                list.add(healthInfo);
            }
            if (list.size() > 0)
            {
                infoCategoryMapper.batchHealthInfo(list);
            }
        }
    }
}
